<template>
  <!-- 试乘试驾查询 -->

  <div>
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="120"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        columnsRef="qjhquery"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :onColumnsChange="columns => (this.columns = columns)"
        :isSelectColumn="false"
      >
        <template slot="controls" slot-scope="props">
          <multiuse-button size="small" type="primary" :click="exportHandle">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>

    <Drawer :visible.sync="isView" :title="'试乘试驾车明细'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <viewModel :rowData="formData" @close="closeViewFn" />
    </Drawer>
    <Drawer :visible.sync="isOrder" :title="'订单信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <orderModel :rowData="formData" @close="closeOrderFn" @save="saveOrderFn" />
    </Drawer>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { vehicleConfiguration } from '@/api/omFinance';
import { exportExcelFile } from '@/utils';
import { queryList, exportExcel,exporturl } from '@/api/dealerSale/TestDrive/query';
import orderModel from './orderModel.vue';
import viewModel from './viewModel.vue';

export default {
  name: 'query',
  mixins: [dictionary],

  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: { isDealer: 1 },
      fetchApi: queryList,
      formData: {},
      selectList: [],
      exportLoading: false,
      isEmit: false,
      isOrder: false,
      isView: false
    };
  },
  components: {
    orderModel,
    viewModel
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getList(); // 加载翻译字典
  },
  methods: {
    viewFn(row) {
      this.formData = row;
      this.isView = true;
    },
    closeViewFn() {
      this.isView = false;
    },
    removeListFn(row) {
      this.$confirm('确认删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //let res = await removeListFn(row);
        this.BaseTable.EXECUTE_DELETE();
      });
    },
    async submitFn(row) {
      //let res = await submitFn(row);
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    getList() {
      this.getvehicleList();
    },
    showOrderFn(vin) {
      this.isOrder = true;
    },
    closeOrderFn() {
      this.isOrder = false;
    },
    saveOrderFn() {
      this.isOrder = false;
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    editHandle(row) {
      this.formData = row;
      this.isEmit = true;
    },
    closeEmitFn() {
      this.isEmit = false;
    },
    saveEmitFn() {
      this.isEmit = false;
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    async getvehicleList() {
      let res = await vehicleConfiguration(0, { isUnlimited: 10041001 });
      this.topFilterList.find(item => item.fieldName === 'brandProductList').itemList = res.data;
    },
    // 导出
    async exportHandle() {
       let json = Object.assign({}, { CODE: 'CODE_0000061' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // await this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     this.exportLoading = true;

      //     exportExcel(this.fetchParams)
      //       .then(response => {
      //         exportExcelFile(response.data, '试乘试驾车查询');
      //         this.exportLoading = false;
      //       })
      //       .catch(err => {
      //         console.log(err);
      //       });
      //   })
      //   .catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消'
      //     });
      //   });
    },

    createdFrom() {
      return [
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          maxlength: '',
          maxRows: 2
        },
        {
          type: 'INPUT_CASCADER',
          label: '车型选择',
          fieldName: 'brandProductList',
          placeholder: '请选择',
          options: { titles: ['品牌', '车系', '车型', '配置', '选装'] },
          itemList: []
        },
        {
          type: 'INPUT',
          label: '申请单号',
          fieldName: 'applyNo',
          placeholder: '请输入'
        },
        // {
        //   type: 'SELECT',
        //   label: '是否首台',
        //   fieldName: 'isFirst',
        //   placeholder: '请选择',
        //   itemList: this.createDictList('1004')
        // },
        {
          type: 'SELECT',
          label: '单据状态',
          fieldName: 'checkStatus',
          placeholder: '请选择',
          itemList: this.createDictList('6076')
        },
        {
          type: 'SELECT',
          label: '是否兑现返利',
          fieldName: 'isRebate',
          placeholder: '请选择',
          itemList: this.createDictList('1004')
        },
        {
          type: 'RANGE_DATE',
          label: '提报日期',
          fieldName: 'reportDateBegin|reportDateEnd',
          dateType: 'exactdaterange'
        },
        {
          type: 'RANGE_DATE',
          label: '事业部审批日期',
          fieldName: 'checkDeptDateBegin|checkDeptDateEnd',
          dateType: 'exactdaterange'
        },
        {
          type: 'RANGE_DATE',
          label: '销售管理部审批日期',
          fieldName: 'checkSalesDateBegin|checkSalesDateEnd',
          dateType: 'exactdaterange'
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.viewFn(props.row)}>
                  明细
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '申请状态',
          dataIndex: 'checkStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6076'),
          render: props => {
            return <span>{this.createDictText(props.row.checkStatus, '6076')}</span>;
          }
        },
        {
          title: '申请单号',
          dataIndex: 'applyNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订单号',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input'
          // render: (props, h) => {
          //   return (
          //     <multiuse-button size="mini" type="text" onClick={() => this.showOrderFn(props.row)}>
          //       {props.row.vin}
          //     </multiuse-button>
          //   );
          // }
        },
        {
          title: '订单类型',
          dataIndex: 'orderType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [
            { text: '在途车', value: 60401002 },
            { text: '库存车', value: 60401003 }
          ],
          render: props => {
            return (
              <span>
                {this.transDictText(props.row.orderType, [
                  { codeCnDesc: '在途车', codeId: 60401002 },
                  { codeCnDesc: '库存车', codeId: 60401003 }
                ])}
              </span>
            );
          }
        },
        {
          title: '品牌',
          dataIndex: 'brandName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车系',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配置',
          dataIndex: 'packageName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装',
          dataIndex: 'optionName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '内饰',
          dataIndex: 'trimName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '外饰',
          dataIndex: 'colorName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品代码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '提报数量',
          dataIndex: 'reportCount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '提报日期',
          dataIndex: 'checkReportDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '事业部审批日期',
          dataIndex: 'checkDeptDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '销售部审批日期',
          dataIndex: 'checkSalesDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        // {
        //   title: '是否首台',
        //   dataIndex: 'isFirst',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'checkbox',
        //   filterItems: this.createDictList('1004'),
        //   render: props => {
        //     return <span>{this.createDictText(props.row.isFirst, '1004')}</span>;
        //   }
        // },
        {
          title: '是否兑现返利',
          dataIndex: 'isRebate',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.isRebate, '1004')}</span>;
          }
        },
        {
          title: '折扣后单价',
          dataIndex: 'unitPrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
